package tech.abing.spider.task.pageprocessor.amac;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.task.model.amac.FundStatEntity;
import tech.abing.spider.task.model.amac.PrivateFundStatEntity;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.UuIdPublic;

public class PrivateFundProcessor implements PageProcessor {

	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		System.out.println(page.getContent());
		
		JSONObject jsonObj = JSONObject.fromObject(page.getContent());
		
		boolean hasPre = jsonObj.getBoolean("hasPre");
		if(!hasPre){//第一页
			long totalPages = jsonObj.getLong("totalPages");
			
			long totalCount = jsonObj.getLong("totalCount");
			PrivateFundStatEntity stat = new PrivateFundStatEntity();
			stat.setId(UuIdPublic.timeStrongID(5));
			stat.setPrivateFundCount(String.valueOf(totalCount));
			stat.setCreateTime(new Date());
			objectContainer.add(stat);
			
			PageRequest request = page.getRequest();
			if(totalPages > 1){
				for(int i = 2; i <= totalPages; i++){
					PageRequest req = context.createPageRequest(request.getUrl(), PrivateFundProcessor.class);
					
					req.setMethod(request.getMethod());
					req.setPostType(request.getPostType());
					
					req.setHeaders(request.getHeaders());
					
					req.putParams("nd", TaskUtil.getRandomStrByDate());
					req.putParams("page.pageNo", String.valueOf(i));
					req.putParams("page.pageSize", "50");
					
					req.putParams("page.searchFileName", "publicity_web");
					req.putParams("page.sqlKey", "PAGE_PUBLICITY_WEB");
					req.putParams("page.sqlCKey", "SIZE_PUBLICITY_WEB");
					req.putParams("_search", "false");
					req.putParams("page.orderBy", "SLRQ");
					req.putParams("page.order", "desc");
					
					req.putParams("filter_LIKES_CPMC", "");
					req.putParams("filter_LIKES_GLJG", "");
					req.putParams("filter_LIKES_CPBM", "");
					req.putParams("filter_GES_SLRQ", "");
					req.putParams("filter_LES_SLRQ", "");
					
					
					queue.add(req);
					
				}
			}
		}
		
		//解析数据
		JSONArray result = jsonObj.getJSONArray("result");
		if(result != null && result.size() > 0){
			for(int i = 0; i < result.size(); i++){
				JSONObject retObj = result.getJSONObject(i);
				String MPI_ID = retObj.getString("MPI_ID");
				
				String url = "http://ba.amac.org.cn/pages/amacWeb/user!search.action";
				PageRequest req = context.createPageRequest(url, PrivateFundDetailProcessor.class);
				
				req.setMethod(Method.POST);
				req.setPostType(PostType.FORMDATA);
				
				req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
				req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
				req.putHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				req.putHeader("Host", "ba.amac.org.cn");
				req.putHeader("Referer", "http://ba.amac.org.cn/pages/amacWeb/web-view.html?id="+MPI_ID);
				req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
				req.putHeader("X-Requested-With", "XMLHttpRequest");
				
				req.putParams("filter_EQS_MPI_ID", MPI_ID);
				req.putParams("sqlkey", "publicity_web");
				req.putParams("sqlval", "GET_PUBLICITY_WEB_BY_MPI_ID");
				
//				req.getExtParams().put("retObj", retObj);
				
				queue.add(req);
			}
		}
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
